package com.zhouhx.flow.demo;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.util.IdUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.zhouhx.flow.cache.DeviceFlowCache;
import com.zhouhx.flow.demo.service.ChargerStartChargingService;
import com.zhouhx.flow.tasklist.TaskManager;
import com.zhouhx.flow.tasklist.entity.FlowConf;
import com.zhouhx.flow.tasklist.entity.FlowInstance;
import com.zhouhx.flow.tasklist.entity.FlowStep;
import com.zhouhx.flow.tasklist.entity.FlowStepTimeOut;
import com.zhouhx.flow.tasklist.enums.FlowStepTypeEnum;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author : zhouhx
 * 普通充电流程
 * @date : 2024/5/3 15:05
 */
public class ChargerControlFlow {
    // 流程id
    private final static String FLOW_ID = "666666";
    private final static String FLOW_NAME = "普通启动充电流程";
    // 开始充电步骤id
    private final static String START_CHARGING_STEPID1 = String.format("%s%s", FLOW_ID, "01");
    private final static String START_CHARGING_STEPID2 = String.format("%s%s", FLOW_ID, "02");

    private final static ChargerStartChargingService startChargingService = SpringUtil.getBean(ChargerStartChargingService.class);

    /**
     * 创建开始充电流程任务
     */
    public static TaskManager crateTaskManager(String equipmentNo){
        FlowConf flowConf = getFlowConf(equipmentNo);
        ConcurrentMap<String, FlowStep> taskMap = getTaskMap();
        FlowInstance flowInstance = getFlowInstance();

        return new TaskManager(flowConf,flowInstance,taskMap);
    }

    /**
     * 获取流程配置
     */
    private  static FlowConf getFlowConf(String equipmentNo) {
        return FlowConf.builder()
                .retry(true)
                .retryCount(3)
                .flowStepTimeOut(new FlowStepTimeOut(10, DateUnit.SECOND))
                .equipmentNo(equipmentNo)
                .build();
    }

    /**
     * 获取流程实例
     */
    private static FlowInstance getFlowInstance() {
        FlowInstance instance = new FlowInstance();
        instance.setFlowInstanceId(IdUtil.getSnowflakeNextIdStr());
        instance.setFlowId(FLOW_ID);
        instance.setFlowName(FLOW_NAME);

        return instance;
    }
    /**
     * 获取任务清单
     */
    private static ConcurrentMap<String, FlowStep> getTaskMap(){
        ConcurrentMap<String, FlowStep> map = new ConcurrentHashMap<>();
        map.put(START_CHARGING_STEPID1,getStartChargingStep1());
        map.put(START_CHARGING_STEPID2,getStartChargingStep2());

        return map;
    }

    /**
     * 步骤一
     */
    private static FlowStep getStartChargingStep1(){
        return FlowStep.builder()
                .flowStepId(START_CHARGING_STEPID2)
                .flowStepName(CommonChargingStatusEnum.STATUS_2.getText())
                .stepType(FlowStepTypeEnum.START.getType())
                // 执行方法
                .excuteFunction((houseNo) -> {
                    ChargerStartChargingVo startChargingVo = new ChargerStartChargingVo();
                    startChargingVo.setHouseNo(houseNo);
                    startChargingService.startCharging(startChargingVo);
                    return CommonChargingStatusEnum.STATUS_2.getStatus();
                })
                // 获取状态的方法
                .stateFunction((houseNo) -> {
                    ChargerStartChargingVo startChargingVo = new ChargerStartChargingVo();
                    startChargingVo.setHouseNo(houseNo);
                    boolean result  = startChargingService.getStartChargingResult(startChargingVo);
                    if(result){
                        return CommonChargingStatusEnum.STATUS_2.getStatus();
                    }
                    return "0";
                })
                .nextFlowStepId(START_CHARGING_STEPID2)
                .build();
    }

    /**
     * 步骤二
     */
    private static FlowStep getStartChargingStep2(){
        return FlowStep.builder()
                .flowStepId(START_CHARGING_STEPID2)
                .flowStepName(CommonChargingStatusEnum.STATUS_2.getText())
                .stepType(FlowStepTypeEnum.END.getType())
                .build();
    }

    public static void main(String[] args) {
        // 添加流程缓存，由页面或者操作动态添加
        DeviceFlowCache.addTaskFlow(ChargerControlFlow.crateTaskManager("5112345"));
    }
}
